import 'package:coinbaby/common/alert/alert.dart';
import 'package:coinbaby/common/textstyle/textstyle.dart';
import 'package:coinbaby/common/util/tool.dart';
import 'package:flutter/material.dart';

class ResetPwdPage extends StatefulWidget {
  @override
  _ResetPwdPageState createState() => _ResetPwdPageState();
}

class _ResetPwdPageState extends State<ResetPwdPage> {

  final _pwdController = TextEditingController();
  final _pwdFocus = FocusNode();

  @override
  void dispose() {
    _pwdController.dispose();
    super.dispose();
  }

  void _submit() {
    if(!Tool.isLoginPassword(_pwdController.text)) {
      Alert.showToast("请输入8-20个字符，不可以是纯数字");
      return;
    }
    int count = 0;
    // 回到密码登陆
    Navigator.popUntil(context, (route) {
      count += 1;
      return count > 2;
    });
  }


  @override
  Widget build(BuildContext context) {
    
    final size = MediaQuery.of(context).size;
    final marginH = size.height * 0.06;

    return Scaffold(
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () => _pwdFocus.unfocus(),
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.keyboard_arrow_left),
                onPressed: () => Navigator.pop(context),
              ),
              SizedBox(height: marginH),
              Padding(
                padding: const EdgeInsets.all(20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text("重新设置密码", style: AppFont.textStyle(26, Colors.black, fontWeight: FontWeight.bold),),
                    SizedBox(height: 10,),
                    Text("8-20个字符，不可以是纯数字", style: AppFont.textStyle(14, Colors.grey),),
                    SizedBox(height: marginH,),
                    TextField(
                      obscureText: true,
                      maxLength: 20,
                      focusNode: _pwdFocus,
                      controller: _pwdController,
                      decoration: InputDecoration(
                        hintText: "请输入密码",
                        counterText: "",
                        hintStyle: AppFont.textStyle(14, Colors.grey[300]),
                        labelStyle: AppFont.textStyle(14, Colors.black),
                      ),
                    ),

                    SizedBox(height: marginH),
                    InkWell(
                      onTap: _submit,
                      child: Container(
                        width: size.width,
                        height: 50,
                        margin: const EdgeInsets.only(top: 35),
                        decoration: BoxDecoration(
                          color: Colors.red,
                          borderRadius: BorderRadius.circular(10)
                        ),
                        alignment: Alignment.center,
                        child: Text("完成", style: AppFont.textStyle(14, Colors.white),),
                      ),
                    ),
                  ]
                )
              ),
            ],
          ),
        ),
      ),
    );
  }
}